﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;

namespace Ebiz.libs
{
    public class Func
    {
        public string SysUName
        {
            get;
            set;
        }

        public enum DTFormat { 
            YMDtoDMY,
            YMDtoMDY,
            DMYtoYMD,
            MDYtoYMD,
            MDYtoDMY
        }
        public enum ShowInfo { 
            _UserID, 
            _PCName,
            _NetworkID,
            _FullName,
            _Email, 
            _RoleID, 
            _RoleName, 
            _MenuID, 
            _SSID, 
            _SSEXP
        }

        public enum SanitizeMode { 
            _Both, 
            _Number, 
            _String, 
            _StripSlash
        }

        public static string regExPattern(SanitizeMode Mode)
        {
            string result = "";
            switch (Mode) { 
                case SanitizeMode._Both:
                    result = @"[^a-zA-Z0-9\s]";
                    break;
                case SanitizeMode._Number:
                    result = @"[^0-9]";
                    break;
                case SanitizeMode._String:
                    result = @"[^a-zA-Z\s]";
                    break;
                case SanitizeMode._StripSlash:
                    result = @"[\000\010\011\012\015\032\042\047\134\140]";
                        break;
            }

            return result;
        }

        public static string Sanitize(string Input) {
            string result = "";
            string _result = HttpUtility.HtmlEncode(Input);
            _result.Trim();
            result = _result;
            
            return result;
        }

        public static string FormatDate(string InputDate, DTFormat DateFormat, string SeparatorFrom, string SeparatorTo){
            string result = "";

            string dtSeparatorFrom = (string.IsNullOrEmpty(SeparatorFrom)) ? "/" : SeparatorFrom;
            string dtSeparatorTo = (string.IsNullOrEmpty(SeparatorTo)) ? "/" : SeparatorTo;
            char separator = dtSeparatorFrom[0];
            string[] ArrDate = InputDate.Split(separator);
            string dtDay;
            string dtMonth;
            string dtYear;
            
            switch (DateFormat) { 
                case DTFormat.YMDtoDMY:
                    dtDay = ArrDate[2];
                    dtMonth = ArrDate[1];
                    dtYear = ArrDate[0];
                    result = dtDay + dtSeparatorTo + dtMonth + dtSeparatorTo + dtYear;
                    break;
                case DTFormat.YMDtoMDY:
                    dtDay = ArrDate[2];
                    dtMonth = ArrDate[1];
                    dtYear = ArrDate[0];
                    result = dtMonth + dtSeparatorTo + dtDay + dtSeparatorTo + dtYear;
                    break;
                case DTFormat.DMYtoYMD:
                    dtDay = ArrDate[0];
                    dtMonth = ArrDate[1];
                    dtYear = ArrDate[2];
                    result = dtYear + dtSeparatorTo + dtMonth + dtSeparatorTo + dtDay;
                    break;
                case DTFormat.MDYtoYMD:
                    dtDay = ArrDate[1];
                    dtMonth = ArrDate[0];
                    dtYear = ArrDate[2];
                    result = dtYear + dtSeparatorTo + dtMonth + dtSeparatorTo + dtDay;
                    break;
                case DTFormat.MDYtoDMY:
                    dtDay = ArrDate[1];
                    dtMonth = ArrDate[0];
                    dtYear = ArrDate[2];
                    result = dtDay + dtSeparatorTo + dtMonth + dtSeparatorTo + dtYear;
                    break;
            }
            return result;
        }

        public static string GetUserInfo(ShowInfo UserInfo){
            string result = "";
            Dictionary<string, object> UInfo = (Dictionary<string, object>)HttpContext.Current.Session["UserInfo"];
            switch (UserInfo) { 
                case ShowInfo._UserID:
                    result = UInfo["UID"].ToString();
                    break;
                case ShowInfo._PCName:
                    result = UInfo["PC"].ToString();
                    break;
                case ShowInfo._NetworkID:
                    result = UInfo["IDNet"].ToString();
                    break;
                case ShowInfo._FullName:
                    result = UInfo["FNAME"].ToString();
                    break;
                case ShowInfo._Email:
                    result = UInfo["EMAIL"].ToString();
                    break;
                case ShowInfo._RoleID:
                    result = UInfo["IDROLE"].ToString();
                    break;
                case ShowInfo._RoleName:
                    result = UInfo["ROLE"].ToString();
                    break;
                case ShowInfo._MenuID:
                    result = UInfo["IDMENU"].ToString();
                    break;
                case ShowInfo._SSID:
                    result = UInfo["SSID"].ToString();
                    break;
                case ShowInfo._SSEXP:
                    result = UInfo["SSEXP"].ToString();
                    break;
            }
            return result;
        }

        public static Dictionary<string, string> GetStatusInfo(string StatusCode) {

            Dictionary<string, string> result = new Dictionary<string, string>();
            string qry = "SELECT Description FROM Ebiz.dbo.mstStatusCode WHERE " + 
                "(StatusCode = @StatusCode)";

            SqlConnection cn = new SqlConnection(DBUtilities.strConnection("devCon"));
            using (cn) {
                try{ 
                    cn.Open();
                    SqlCommand cmd = new SqlCommand(qry, cn);
                    cmd.Parameters.AddWithValue("@StatusCode", StatusCode);
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows) {
                        if (rdr.Read()) {
                            result.Add("CODE", StatusCode);
                            result.Add("DESCRIPTION", rdr.GetValue(0).ToString());
                        }
                    }
                }
                catch (Exception ex) {
                    string errMsg = ex.Message;
                }
                finally {
                    if (cn.State == ConnectionState.Open) {
                        cn.Close();
                    }
                }

            }
            return result;
        }

        public static Dictionary<string, object> GetSysConfig(){
            string dbName = DBUtilities.GetDBName("devCon");
            Dictionary<string, object> result = new Dictionary<string, object>();
            string qry = "SELECT ConfigName, ConfigValue FROM " + dbName + ".dbo.mstConfig WHERE " +
                "(ConfigGroup = 'System')";

            SqlConnection cn = new SqlConnection(DBUtilities.strConnection("devCon"));
            using (cn){
                try{
                    cn.Open();
                    SqlCommand cmd = new SqlCommand(qry, cn);
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows){
                        while (rdr.Read()){
                            result.Add(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString());
                        }
                    }
                }catch (Exception ex){
                    string errMsg = ex.Message;
                }finally{
                    if (cn.State == ConnectionState.Open){
                        cn.Close();
                    }
                }
            }
            return result;
        }

        public static int strlen(string input) {
            StringBuilder cb = new StringBuilder(input);
            string chrs;

            cb.Append('\0');
            cb.Append("def");

            chrs = cb.ToString();
            return chrs.Length;
        }

        /// <summary>
        /// Get Domain Name from String
        /// </summary>
        /// <remarks>Return Domain Name without HTTP prefix</remarks>
        /// <returns>string</returns>
        public static string GetDomainFromString(string DomainName) {
            return GetDomainStepOne(DomainName, false);
        }

        /// <summary>
        /// Get Domain Name from String
        /// </summary>
        /// <remarks>bool UseHTTP, Return Domain Name with/without HTTP prefix</remarks>
        /// <returns>string</returns>
        public static string GetDomainFromString(string DomainName, bool UseHTTP) {
            return GetDomainStepOne(DomainName, UseHTTP);
        }

        private static string GetDomainStepOne(string DomainName, bool UseHTTP) {
            string result = "";

            string InsertHTTP = (UseHTTP == true) ? "http://" : "";
            string[] ExplodeText = DomainName.Split(':');
            int ExplodeLen = ExplodeText.Length;
            if (ExplodeLen > 1){
                string _domainName = ExplodeText[(ExplodeLen - 1)];
                result = InsertHTTP + GetDomainStepTwo(_domainName);
            }else{
                result = InsertHTTP + GetDomainStepTwo(DomainName);
            }
            return result;
        }

        private static string GetDomainStepTwo(string DomainName) {
            string result = "";

            string[] ExplodeText = DomainName.Split('/');
            int ExplodeLen = ExplodeText.Length;
            for (int idx = 0; idx < ExplodeLen; idx++){
                if (!(string.IsNullOrEmpty(ExplodeText[idx])) && ExplodeText[idx] != "/"){
                    result = ExplodeText[idx];
                    break;
                }
            }
            return result;
        }
    }
}
